package pathways.SpeciesSpace;

/**
 * Title:        Pathways
 * Description:  Pure Java version of the Pathways Project.
 * Copyright:    Copyright (c) 2002
 * Company:      Ants Technologies
 * @author Veejay Sani
 * @version 1.0
 */
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

import pathways.Database.Implementation.CDatabase;
import pathways.util.CDBUtil;
import pathways.util.Debug.CDebug;
import pathways.util.helper.CItem;

public class CInstantiation
{
  private CSpace space = new CSpace();
  private int reactionID = 0;
  private CItem species = new CItem(0,"");
  private int ID = 0;
  private Vector data = new Vector();
  
  public CInstantiation(CDatabase db)
  {
    Vector v = CDBUtil.getDataFields(db);
    CData d = null;
    CField fld = null;
    
    for (int i = 0; i < v.size(); i++)
    {
      fld = (CField)v.get(i);
      
      d = new CData();
      d.setField(fld);
        
      data.add(d);
    }
  }
  
  public CSpace getSpace()
  {
    return space; 
  }
  
  public void setSpace(CSpace space)
  {
    this.space = space; 
  }
  
  public int getReactionID()
  {
    return reactionID; 
  }
  
  public void setReactionID(int rxnID)
  {
    reactionID = rxnID; 
  }
  
  public CItem getSpecies()
  {
    return species; 
  }
  
  public void setSpecies(CItem species)
  {
    this.species = species; 
  }
  
  public int getID()
  {
    return ID; 
  }
  
  public Vector getData()
  {
    return data;  
  }
  
  public boolean read(int id, CDatabase db)
  {
    ResultSet inst = db.getSelect().getInstantiation(id);
    int spaceID = 0;
    int taxid = 0;
    
    try{
      if (!inst.next())
      {
        return false;
      }
      
      ID = inst.getInt(1);
      spaceID = inst.getInt(2);
      reactionID = inst.getInt(3);
      taxid = inst.getInt(4);
         
    } catch (SQLException sqle) {
      CDebug.println(this, "read() Exception: " + sqle.toString());
      return false;
    }
    
    // read species
    ResultSet rsSpecies = db.getSelect().getSpecies(taxid);
    
    try {
      rsSpecies.next();
      species.setID(rsSpecies.getInt(1));
      species.setValue(rsSpecies.getString(2));
      
    } catch (SQLException sqle) {
      CDebug.println(this, "read() Exception: " + sqle.toString());
      return false;  
    }
    
    // read space
    if (!space.read(spaceID,db))
       return false;
    
    // read data
    if (!readData(ID,db))
       return false;
    
    return true; 
  }
  
  public boolean readData(int instRxnID, CDatabase db)
  {
    // reads all fields
    // if data does not exist for field then reads
    // empty string 
    ResultSet rs = db.getSelect().getInstantiationData(instRxnID);
    int dataID = 0;
    int fieldID = 0;
    String fieldValue = "";
    CData tempData = null;
    
    data.removeAllElements();
    
    try {
      while (rs.next())
      {
        fieldID = rs.getInt(1);
        fieldValue = rs.getString(2);
        if (rs.getObject(3) == null)
           dataID = 0;
        else
           dataID = rs.getInt(3);
        
        tempData = new CData();
        
        if (dataID > 0)
        {
          tempData.read(dataID, db);
        }
        else
        {
          tempData.getField().setID(fieldID);
          tempData.getField().setName(fieldValue);
        }
        
        data.add(tempData);
      }
    } catch (SQLException sqle) {
      CDebug.println(this,"readData() Exception: " + sqle.toString());
      return false;
    }
    
    return true;
  }
  
  public boolean save(CDatabase db)
  {
    // save the space
    // save the instantiation
    // save the data
    
    if (!space.save(db))
       return false;
    
    if (ID > 0)
    {
      if (!db.getUpdate().updateInstantiation(ID,space.getID(),species.getID()))
         return false;  
    }
    else
    {
      ID = db.getUpdate().newInstantiatedRxn(space.getID(),reactionID,species.getID());
      
      if (ID <= 0)
         return false;
    }
    
    if (!saveData(db))
       return false;
    
    return true;
  }
  
  private boolean saveData(CDatabase db)
  {
    CData tempData = null;
    
    for (int i = 0; i < data.size(); i++)
    {
      tempData = (CData)data.get(i);
      tempData.setInstantiatedReactionID(ID);
       
      tempData.save(db);    
    }
    
    return true;   
  }
  
}